package cn.edu.scau.cmi.liangzaoqing.hibernate.dao;

import java.util.List;
import java.util.Set;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;


import cn.edu.scau.cmi.liangzaoqing.hibernate.domain.Major;
import cn.edu.scau.cmi.liangzaoqing.util.HibernateSessionFactoryUtil;
//！！！MajorDAOByJdbc的具体数据访问方式采用的是JDBC，Hibernate的DAO子包的DAO的数据访问方式采用的是Hibernate中的Session对象的方式
public class MajorDAO{
	// property constants
	public static final String NAME = "name";
	
	private HibernateSessionFactoryUtil sessionFactoryUtil;
	
	public HibernateSessionFactoryUtil getSessionFactoryUtil() {
		return sessionFactoryUtil;
	}

	public void setSessionFactoryUtil(HibernateSessionFactoryUtil sessionFactory) {
		this.sessionFactoryUtil = sessionFactory;
	}
	

	/* 
	 * 这个是比较完整的DAO的方法 
	 * 
	 * 
	 * 
	 * */
	public void save(Major transientInstance) {
		try {
			Session session = sessionFactoryUtil.getSession();
			Transaction transaction = session.beginTransaction();
			session.save(transientInstance);
			transaction.commit();
			session.close();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(Major persistentInstance) {
		try {
			sessionFactoryUtil.getSession().delete(persistentInstance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Major findById(java.lang.Long id) {
		try {
			Major instance = (Major) sessionFactoryUtil.getSession().get("cn.edu.scau.cmi.liangzaoqing.hibernate.Major", id);
			return instance;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(Major instance) {
		try {
			List results = sessionFactoryUtil.getSession().createCriteria("cn.edu.scau.cmi.liangzaoqing.hibernate.Major")
					.add(Example.create(instance)).list();
			return results;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		try {
			String queryString = "from Major as model where model." + propertyName + "= ?";
			Query queryObject = sessionFactoryUtil.getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findAll() {
		try {
			String queryString = "from Major";
			Query queryObject = sessionFactoryUtil.getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public Major merge(Major detachedInstance) {
		try {
			Major result = (Major) sessionFactoryUtil.getSession().merge(detachedInstance);
			return result;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void attachDirty(Major instance) {
		try {
			sessionFactoryUtil.getSession().saveOrUpdate(instance);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void attachClean(Major instance) {
		try {
			sessionFactoryUtil.getSession().buildLockRequest(LockOptions.NONE).lock(instance);
		} catch (RuntimeException re) {
			throw re;
		}
	}


}